package com.sujing.webService.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sujing.webService.impl.HelloServiceImpl;

/**
 * Servlet implementation class webServiceServlet
 */
@WebServlet("/webServiceInsertServlet")
public class webServiceInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public webServiceInsertServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		System.out.println("...................init().........................");
		System.out.println("..................."+config.getInitParameter(getServletInfo())+".........................");
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("...................service().........................");
		String name=request.getParameter("name");
		String pwd=request.getParameter("password");
		System.out.println(".........name value:........."+name);
		System.out.println("........password value:..........."+pwd);
		if(name!=""&&pwd!=""){
			HelloServiceImpl helloServiceI=new HelloServiceImpl();
			String json=helloServiceI.insert(name, pwd);
			System.out.println("json value................"+json);
			if(json.equals("{result:0}")){
				request.setAttribute("json", "{result:0}");
				doPost(request, response);
			}else{
				request.setAttribute("json", "{result:1}");
				doPost(request, response);
			}
		}else{
			request.getRequestDispatcher("noName.jsp").forward(request, response);
		}
	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		/*char[] ch=returns.toCharArray();
		for(int i=0;i<=ch.length;i++){
			response.getWriter().append(ch[i]);
		}*/
		doPost(request, response);
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("...................post().........................");
		String json=(String) request.getAttribute("json");
		System.out.println("...............json value:............."+json);
		PrintWriter out=response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
        out.println("<html>");  
        out.println("<body>");  
        out.println(json);  
        out.println("</body>");  
        out.println("</html>");   
	}

	/**
	 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
	 */
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
